package com.cen.cloud.controller;

import com.cen.cloud.pojo.Dept;
import com.cen.cloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author cen
 * @Date 2020/10/4 16:29
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/dept/get")
    public List<Dept>getAll(){
        return deptService.getAll();
    }

    @GetMapping("/dept/getByID/{id}")
    @HystrixCommand(fallbackMethod = "hstrixGetByID")
    public Dept getByID(@PathVariable("id") Long deptno){
        Dept byID = deptService.getByID(deptno);
        if (byID==null){
            throw new RuntimeException("没得了");
        }else {
            return byID;
        }
    }


    public Dept hstrixGetByID(@PathVariable("id") Long id){
        return new Dept()
                .setDeptno(id)
                .setDname("不存在，报错了")
                .setDb_source("数据苦也不在");
    }
}
